package atm05;

import atm04.Users;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 登录界面
 */
public class LoginPanel extends JPanel implements ActionListener{
    private ATM05 atm05;
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JButton loginBtn = new JButton(new ImageIcon("src/atm05/Images/buttonLand.jpg"));
    private JButton exitBtn = new JButton(new ImageIcon("src/atm05/Images/buttonOff.jpg"));
    private Font font = new Font("黑体",Font.BOLD,25);
    private int change = 3;

    public LoginPanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        accountLabel.setBounds(300,200,80,50);
        accountLabel.setFont(font);


        accountTextField.setBounds(380,200,250,50);
        accountTextField.setFont(font);

        pwdLabel.setBounds(300,250,80,50);
        pwdLabel.setFont(font);

        pwdField.setBounds(380,250,250,50);

        loginBtn.setBounds(420,320,140,50);
        loginBtn.addActionListener(this);


        exitBtn.setBounds(880,380,140,50);
        exitBtn.addActionListener(this);


        this.add(accountLabel);
        this.add(accountTextField);
        this.add(pwdLabel);
        this.add(pwdField);
        this.add(loginBtn);
        this.add(exitBtn);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
            if (e.getSource() == loginBtn){
                ArrayList<Users> users = atm05.getUsers();
                String account = accountTextField.getText();
                String pwd = new String(pwdField.getPassword());
                for (int i = 0; i < users.size(); i++){
                    if (users.get(i).getAccount().equals(account) && users.get(i).getPwd().equals(pwd)){
                        atm05.setLoginUser(users.get(i));
                        atm05.showPanel("menu");
                        accountTextField.setText("");
                        pwdField.setText("");
                        return;
                    }
                }
                change--;
                if (change > 0){
                    JOptionPane.showMessageDialog(null,"您输入的账号或密码有误，请重新输入（还有"+ change + "次机会)");
                    accountTextField.setText("");
                    pwdField.setText("");
                }else {
                    JOptionPane.showMessageDialog(null,"你登录次数已用完");
                    System.exit(0);
                }
            }else if (e.getSource() == exitBtn){
                System.exit(0);
            }
        }
    }
